package com.atguigu.cloud.controller;

import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.resp.ResultData;
import jakarta.annotation.Resource;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author 23087
 */
@RestController
@RequestMapping("/consumer")
public class OrderController {

    // 先硬编码写死
//    public static final String PaymentSrv_URL = "http://localhost:8001";
    // 服务注册中心上的微服务名称
    public static final String PaymentSrv_URL = "http://cloud-payment-service";

    @Resource
    private RestTemplate restTemplate;

    /**
     * 新增
     * @param payDTO
     * @return
     */
    @GetMapping("/pay/add")
    public ResultData addOrder(PayDTO payDTO){
        /**
         * 参数：url,请求参数，返回类型
         */
       return restTemplate.postForObject(PaymentSrv_URL + "/pay/add",payDTO,ResultData.class);
    }

    /**
     * 根据 id 查询
     * @param id
     * @return
     */
    @GetMapping("/pay/get/{id}")
    public ResultData getPayInfo(@PathVariable("id") Integer id){
        /**
         * 参数：url,返回类型,请求参数
         */
        return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/" + id,ResultData.class,id);
    }

    // todo 删除/修改
    /**
     * 根据 id 删除
     * @param id
     * @return
     */
    @GetMapping("/pay/del/{id}")
    public ResultData delPayInfo(@PathVariable("id") Integer id){
        // 使用 delete 发送 DELETE 请求，返回值为 void ，方法 public 后换成 void
        // restTemplate.delete(PaymentSrv_URL + "/pay/del/" + id,99);
        /**
         * 使用 exchange 发送 DELETE 请求，有返回值
         * 参数： url,请求方式，请求参数，返回类型
         */
        ResponseEntity<ResultData> result = restTemplate.exchange(PaymentSrv_URL + "/pay/del/" + id, HttpMethod.DELETE, null, ResultData.class);
        return result.getBody();
    }

    /**
     * 更新
     * @param payDTO
     * @return
     */
    @GetMapping("/pay/update")
    public ResultData updatePayInfo(PayDTO payDTO){
        // restTemplate.put(PaymentSrv_URL + "/pay/update",payDTO,99);
        // 请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        // 发送请求
        HttpEntity<PayDTO> httpEntity = new HttpEntity<>(payDTO, headers);
        ResponseEntity<ResultData> result = restTemplate.exchange(PaymentSrv_URL + "/pay/update", HttpMethod.PUT, httpEntity, ResultData.class);
        return result.getBody();
    }

    /**
     * 测试 LoadBalancer 本地负载均衡
     * @return
     */
    @GetMapping("/pay/get/info")
    public String getInfoByConsul(){
        return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/info", String.class);
    }

// ==============================================================================================================================
    @Resource
    private DiscoveryClient discoveryClient;
    @GetMapping("/discovery")
    public String discovery()
    {
        List<String> services = discoveryClient.getServices();
        for (String element : services) {
            System.out.println(element);
        }

        System.out.println("===================================");

        List<ServiceInstance> instances = discoveryClient.getInstances("cloud-payment-service");
        for (ServiceInstance element : instances) {
            System.out.println(element.getServiceId()+"\t"+element.getHost()+"\t"+element.getPort()+"\t"+element.getUri());
        }

        return instances.get(0).getServiceId()+":"+instances.get(0).getPort();
    }

}
